-- 快速交接任务
local function TuiGetQuests(...)
    local n = 1
    local QuestList = {}
    for i = 1, select('#', ...), 7 do
        QuestList[n] = select(i + 3, ...)
        n = n + 1
    end
    return QuestList
end
local function TuiCompleteQuests()
    local CompleteButton = _G['QuestFrameCompleteQuestButton']
    if CompleteButton then
        CompleteButton:GetScript('OnClick')()
    else
        print('获取按钮失败!')
    end
end

local function TuiGetGossipActiveQuestInfo(index)
    if not index then
        return
    end
    index = index == 1 and 1 or (index - 1) * 7 + 1
    -- title# 
    -- String - The name of the quest
    -- level# 
    -- Number - The level of the quest
    -- isLowLevel# 
    -- Boolean - true if the quest is low level, false otherwise
    -- isComplete# 
    -- Boolean - true if the quest is complete, false otherwise
    -- isLegendary# 
    -- Boolean - true if the quests is a legendary quest, false otherwise
    -- isIgnored# 
    -- Boolean - true if the quests has been ignored, false otherwise
    -- questID# 
    -- Number - QuestID of the quest
    -- local
    local title, level, isLowLevel, isComplete, isLegendary, isIgnored, questID = select(index, GetGossipActiveQuests())
    return title, level, isLowLevel, isComplete, isLegendary, isIgnored, questID
end
local function TuiGetGossipAvailableQuestInfo(index)
    if not index then
        return
    end
    index = index == 1 and 1 or (index - 1) * 8 + 1
    --   title 
    -- String - The name of the quest.
    -- level 
    -- Number - The level of the quest.
    -- isTrivial 
    -- Boolean - true if the quest is trivial (too low-level compared to the character), false otherwise.
    -- frequency 
    -- Integer - 1 if the quest is a normal quest, LE_QUEST_FREQUENCY_DAILY (2) for daily quests, LE_QUEST_FREQUENCY_WEEKLY (3) for weekly quests.
    -- isRepeatable 
    -- Boolean - true if the quest is repeatable, false otherwise.
    -- isLegendary 
    -- Boolean - true if the quest is a legendary quest, false otherwise.
    -- isIgnored 
    -- Boolean - true if the quest has been ignored, false otherwise.
    -- questID# 
    -- Number - QuestID of the quest
    local title, level, isTrivial, frequency, isRepeatable, isLegendary, isIgnored, questID =
        select(index, GetGossipAvailableQuests())
    return title, level, isTrivial, frequency, isRepeatable, isLegendary, isIgnored, questID
end
TuiQuests = CreateFrame('Frame')
TuiQuests.Events = {
    ['GOSSIP_SHOW'] = function()
        if not TuiDB.IsQuests then
            return
        end
        local NumGossipActiveQuests = GetNumGossipActiveQuests()
        local NumGossipAvailableQuests = GetNumGossipAvailableQuests()

        if (NumGossipActiveQuests ~= 0) then
            local QuestList = TuiGetQuests(GetGossipActiveQuests())
            for i = 1, NumGossipActiveQuests do
                local title, level, isLowLevel, isComplete, isLegendary, isIgnored, questID =
                    TuiGetGossipActiveQuestInfo(i)
                if isComplete then
                    SelectGossipActiveQuest(i)
                end
            end
        end
        if (NumGossipAvailableQuests ~= 0) then
            for i = 1, NumGossipAvailableQuests do
                local title, level, isTrivial, frequency, isRepeatable, isLegendary, isIgnored, questID =
                    TuiGetGossipAvailableQuestInfo(i)
                if not isTrivial and frequency == 1 and not isRepeatable then
                    SelectGossipAvailableQuest(i)
                end
            end
        end

    end,
    ['QUEST_GREETING'] = function()

        if not TuiDB.IsQuests then
            return
        end
        local NumActiveQuests = GetNumActiveQuests()
        local NumAvailableQuests = GetNumAvailableQuests()

        if NumActiveQuests ~= 0 then
            for i = 1, NumActiveQuests do
                local _, isComplete = GetActiveTitle(i)
                if isComplete then
                    SelectActiveQuest(i)
                end
            end
        end
        if (NumAvailableQuests ~= 0) then
            for i = 1, NumAvailableQuests do
                -- local isTrivial, frequency, isRepeatable, isLegendary = GetAvailableQuestInfo(i)
                -- if not isTrivial and frequency == 1 and not isRepeatable then
                SelectAvailableQuest(i)
                -- end
            end

        end

    end,
    ['QUEST_DETAIL'] = function()
        if not TuiDB.IsQuests then
            return
        end
        AcceptQuest()
    end,
    ['QUEST_PROGRESS'] = function()

        if not TuiDB.IsQuests then
            return
        end
        if (IsQuestCompletable()) then
            CompleteQuest()
        end
    end,
    ['QUEST_COMPLETE'] = function()

        if not TuiDB.IsQuests then
            return
        end

        local NumQuestChoices = GetNumQuestChoices()
        TuiCompleteQuests()
        if (NumQuestChoices > 0) then
            print('请选择任务品! 选择最优任务品, 正在开发中...')
        end
    end
}
